<script setup lang="ts">
import {store} from "@/modal/store";
import Fruit, {Apple, Blackberry, Peach} from "@/modal/Fruit";
import FruitItem from "@/components/FruitType/FruitType.vue";
import {computed, type Reactive, reactive, ref, watch} from "vue";
import {ActionName} from "@/components/type";
import Score from "@/modal/Score";
import Buff from "@/modal/Buff";
import Action from "@/modal/Action";
import IconFont from "@/components/IconFont/IconFont.vue";
import {IconColor, IconType} from "@/components/IconFont/type";
import Goods from "@/modal/Goods";

const score = reactive(Score.getInstance());
const goods = reactive(Goods.getInstance());
const action = reactive(Action.getInstance());
const buff = reactive(Buff.getInstance());
const tools = store.tools;
// 获取果园实例
const fruits: Reactive<Fruit[]> = store.fruits;
// 执行水果区域行动
let ripenFruitsNumber = ref(0);
const executeFruitAreaAction = (modal: Reactive<Fruit>) => {
  if (action.fruitTimes === 0) return;
  if (!modal.isRipen) {
    modal.growUp().getAward();
    action.useAction(ActionName.FRUIT)
    // 累加收获的水果个数
    if (modal.ripenSteps) ripenFruitsNumber.value++;
  }
  // growUp会使isRipen变为true，所以这里不能用else
  if (modal.ripenSteps) setFruitBuff(modal);
}

// 上buff
// 郁金香buff
const tulipScoreBuff = computed(() => {return buff.tulipScore > 0;});
const tulipGoodsBuff = computed(() => {return buff.tulipGoods > 0;});
// 玻璃罐buff
const glassGoodsBuff = computed(() => {return buff.glassGoods > 0;});
// 馅饼柜buff
const pieGoodsBuff = computed(() => {return buff.pieGoods > 0;});
const saladBuff = computed(() => {return buff.saladBuff;});
const setFruitBuff = (modal: Reactive<Fruit>) => {
  if (tulipScoreBuff.value) score.getTupleScore(ripenFruitsNumber.value);
  if ((tulipGoodsBuff.value || pieGoodsBuff.value) && modal instanceof Peach) goods.peachSwitchGoods();
  if (glassGoodsBuff.value && modal instanceof Blackberry) goods.blackberrySwitchGoods();
  if (pieGoodsBuff.value && modal instanceof Apple) goods.appleSwitchGoods();
  if (saladBuff.value) score.getSaladScore(store.fruits);
}
// 监听郁金香成熟buff，并立即触发;
watch(tulipScoreBuff, (value, oldValue) => {
  if (value) score.getTupleScore(ripenFruitsNumber.value);
}, {once: true});
watch(saladBuff, (value, oldValue) => {
  if (value) score.getSaladScore(store.fruits);
}, {once: true});

</script>


<template>
  <div class="fruit container-fluid col-12 h-100">
    <div class="fruit-title user-select-none">
      <icon-font :icon-type="IconType.FRUIT"
                 :color="IconColor.IRRIGATE"
                 :is-desc="true"
                 font-size="xx-large">
        <template #description>
          <span>果园</span>
        </template>
      </icon-font>
    </div>
    <div class="fruit-main">
      <fruit-item v-for="items in fruits"
                  :fruit="items"
                  @emitGrowUp="executeFruitAreaAction">
      </fruit-item>
    </div>
  </div>
</template>

<style scoped>
.fruit-title {
  height: 10%;
}

.fruit-main {
  height: 90%;
}
</style>